package com.woniu.mealsService.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.woniu.annotation.RequireRole;
import com.woniu.dto.StoreListDTO;
import com.woniu.dto.StoreProductDTO;
import com.woniu.form.*;
import com.woniu.mealsService.model.Store;
import com.woniu.mealsService.model.StoreProduct;
import com.woniu.mealsService.service.StoreProductService;
import com.woniu.mealsService.service.StoreService;
import com.woniu.param.*;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 店铺菜品表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2024年12月10日
 */
@RestController
@RequestMapping("/storeProduct")
@Api(tags = "店铺菜品管理")
@Validated
public class StoreProductController {
    @Autowired
    private StoreProductService storeProductService;
    //菜品管理

    //添加菜品
    @ApiOperation(value = "添加菜品")
    @PostMapping("/addStoreProduct")
    @RequireRole({"商家"})
    public Result addStoreProduct(@RequestHeader String token,@Valid @RequestBody InsertStoreProductForm insertStoreProductForm) throws Exception {
        System.out.println(insertStoreProductForm);
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.addStoreProduct(BeanUtil.toBean(insertStoreProductForm, InsertStoreProductParam.class),userId);
        return Result.getResult().setMsg("添加菜品成功").setCode(200);
    }
    @ApiOperation(value = "修改菜品基本信息")
    @PostMapping("/updateStoreProduct")
    @RequireRole({"商家"})
    public Result updateStoreProduct(@RequestHeader String token,@Valid @RequestBody UpdateStoreProductForm updateStoreProductForm) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.updateStoreProduct(BeanUtil.toBean(updateStoreProductForm, UpdateStoreProductParam.class),userId);
        return Result.getResult().setMsg("修改菜品成功").setCode(200);
    }
    @ApiOperation(value = "修改折扣")
    @PostMapping("/updateDiscount")
    @RequireRole({"商家"})
    public Result updateDiscount(@RequestHeader String token,@Valid @RequestBody UpdateDiscountForm updateDiscountForm) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.updateDiscount(BeanUtil.toBean(updateDiscountForm, UpdateDiscountParam.class),userId);
        return Result.getResult().setMsg("修改折扣成功").setCode(200);
    }
    @ApiOperation(value = "上架商品")
    @ApiImplicitParam(name="productId",value="商品id")
    @GetMapping("/putOn")
    @RequireRole({"商家"})
    public Result putOn(@RequestHeader String token,
                        @NotNull(message = "商品id不能为空")
                        @Positive(message = "商品id必须为正整数")
                        @RequestParam
                        Long productId
    ) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.putOn(productId,userId);
        return Result.getResult().setMsg("上架成功").setCode(200);
    }
    @ApiOperation(value = "下架商品")
    @ApiImplicitParam(name = "productId",value="商品id")
    @GetMapping("/putOff")
    @RequireRole({"商家"})
    public Result putOff(@RequestHeader String token,
                         @NotNull(message = "商品id不能为空")
                         @Positive(message = "商品id必须为正整数")
                         @RequestParam
                         Long productId
    ) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.putOff(productId,userId);
        return Result.getResult().setMsg("下架成功").setCode(200);
    }
    @ApiOperation(value = "删除商品")
    @ApiImplicitParam(name = "productId",value="商品id")
    @DeleteMapping("/deleteProduct")
    @RequireRole({"商家"})
    public Result deleteProduct(@RequestHeader String token,
                                @NotNull(message = "商品id不能为空")
                                @Positive(message = "商品id必须为正整数")
                                @RequestParam("productId")
                                Long productId
    ) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.deleteProduct(productId,userId);
        return Result.getResult().setMsg("删除成功").setCode(200);
    }
    @ApiOperation(value = "查询店铺商品列表")
    @PostMapping("/searchStoreProductList")
    @RequireRole({"商家","学生"})
    public Result searchStoreProductList(@RequestHeader String token,@Valid @RequestBody StoreProductListForm storeProductListForm) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        PageInfo<StoreProductDTO> pageInfo=storeProductService.searchStoreProductList(userId,BeanUtil.toBean(storeProductListForm, StoreProductListParam.class));
        return Result.getResult(PageInfo.class).setData(pageInfo);
    }
    @ApiOperation(value = "查询店铺折扣商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "store_id",value="店铺id"),
            @ApiImplicitParam(name = "pageNum",value="当前页码"),
            @ApiImplicitParam(name = "pageSize",value="每页显示的数量")
    })
    @GetMapping("/searchStoreDiscountProductList")
    @RequireRole({"商家","学生"})
    public Result searchStoreDiscountProductList(@NotBlank(message = "店铺id不能为空")
                                                 @RequestParam
                                                 String storeId,
                                                 @NotNull(message = "当前页码不能为空")
                                                 @Positive(message = "当前页码必须为正整数")
                                                 @RequestParam
                                                 Integer pageNum,
                                                 @NotNull(message = "每页显示的数量不能为空")
                                                 @Positive(message = "每页显示的数量必须为正整数")
                                                 @RequestParam
                                                 Integer pageSize
    ) throws Exception {
        PageInfo<StoreProductDTO> pageInfo=storeProductService.searchStoreDiscountProductList(storeId,pageNum,pageSize);
        return Result.getResult(PageInfo.class).setData(pageInfo);
    }
    @ApiOperation(value = "查询所有商品列表")
    @PostMapping("/searchAllStoreProductList")
    @RequireRole({"商家","学生"})
    public Result searchAllProductList(@Valid @RequestBody AllProductListForm allProductListForm) throws Exception {
        PageInfo<StoreProductDTO> pageInfo=storeProductService.searchAllProductList(BeanUtil.toBean(allProductListForm, AllProductListParam.class));
        return Result.getResult(PageInfo.class).setData(pageInfo);
    }
    @ApiOperation(value = "查询所有折扣商品列表")
    @PostMapping("/searchAllDiscountProductList")
    @RequireRole({"商家","学生"})
    public Result searchAllDiscountProductList(@Valid @RequestBody PageForm pageForm) throws Exception {
        PageInfo<StoreProductDTO> pageInfo=storeProductService.searchAllDiscountProductList(BeanUtil.toBean(pageForm, PageParam.class));
        return Result.getResult(PageInfo.class).setData(pageInfo);
    }
    @ApiOperation(value = "根据商品id查询店铺商品详情")
    @ApiImplicitParam(name = "productId",value="商品id")
    @GetMapping("/searchStoreProductDetail")
    @RequireRole({"商家","学生"})
    public Result searchStoreProductDetail(@NotBlank(message = "商品id不能为空")
                                           @RequestParam
                                           String productId
    ) throws Exception {
        StoreProductDTO storeProduct=storeProductService.searchStoreProductDetail(productId);
        return Result.getResult(StoreProductDTO.class).setData(storeProduct);
    }
//    @ApiOperation(value = "模糊查询商品")
//    @ApiImplicitParam(name = "productName",value="商品名称")
//    @GetMapping("/searchStoreProductDetailByName")
//    public Result searchStoreProductDetailByName(@NotBlank(message = "商品名称不能为空")
//                                           @RequestParam
//                                           String productName,
//                                         @RequestParam
//                                         @Min(value = 1,message = "页码必须为正整数")
//                                         Integer pageNum,
//                                         @RequestParam
//                                         @Min(value = 1,message = "每页行数必须为正整数")
//                                         Integer pageSize
//    ) throws Exception {
//        Page<StoreProduct> page = storeProductService.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<StoreProduct>().like(StoreProduct::getProductName, productName));
//        com.woniu.utils.PageInfo<StoreProductDTO> storeProductDTOPageInfo = new com.woniu.utils.PageInfo<>(page, StoreProductDTO.class);
//        return Result.getListResult(StoreProductDTO.class).setData().setCode(200).setMsg("查询餐品成功");
//    }

    //购物车管理
    @ApiOperation(value = "将商品添加至购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId",value = "商品id"),
            @ApiImplicitParam(name = "num",value = "商品数量")
    })
    @GetMapping("/addCart")
    @RequireRole({"学生"})
    public Result addCart(@RequestHeader String token,
                          @NotBlank(message = "商品id不能为空")
                          @RequestParam
                          String productId,
                          @NotNull(message = "商品数量不能为空")
                          @Positive(message = "商品数量必须为正整数")
                          @RequestParam
                          Integer num
    ) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.addCart(productId,userId,num);
        return Result.getResult().setMsg("购物车添加商品成功").setCode(200);
    }

    @ApiOperation(value = "查询用户购物车列表")
    @PostMapping("/searchCarts")
    @RequireRole({"学生"})
    public Result searchCarts(@RequestHeader String token) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        List<Map<String,Object>> maps=storeProductService.searchCarts(userId);
        return Result.getResult(List.class).setMsg("查询购物车列表成功").setCode(200).setData(maps);
    }
    @ApiOperation(value = "查询对应用户对应商家的购物车列表")
    @ApiImplicitParam(name = "storeId",value = "商家id")
    @GetMapping("/searchCartByStoreId")
    @RequireRole({"学生"})
    public Result searchCartByStoreId(@RequestHeader String token,
                                      @NotBlank(message = "商家id不能为空")
                                      @RequestParam
                                      String storeId
    ) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        Map<String, Object> products=storeProductService.searchCartByStoreId(userId,storeId);
        return Result.getResult(Map.class).setMsg("查询该商家购物车成功").setCode(200).setData(products);
    }
    @ApiOperation(value = "清空该店铺购物车")
    @ApiImplicitParam(name = "storeId",value = "商家id" )
    @DeleteMapping("/clearCart")
    @RequireRole({"学生"})
    public Result clearCart(@RequestHeader String token,
                            @NotBlank(message = "商家id不能为空")
                            @RequestParam
                            String storeId
    ) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.clearCart(userId,storeId);
        return Result.getResult().setMsg("清空该店铺购物车成功").setCode(200);
    }
    @ApiOperation(value = "移除购物车商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId",value = "商品id"),
            @ApiImplicitParam(name = "num",value = "商品数量")
    })
    @DeleteMapping("/removeCart")
    @RequireRole({"学生"})
    public Result removeCart(@RequestHeader String token,
                             @NotBlank(message = "商品id不能为空")
                             @RequestParam
                             String productId,
                             @NotNull(message = "商品数量不能为空")
                             @Positive(message = "商品数量必须为正整数")
                             @RequestParam
                             Integer num
    ) throws Exception {
        String userId = JwtUtil.parseToken(token).get("userId").toString();
        storeProductService.removeCart(userId,productId,num);
        return Result.getResult().setMsg("移除购物车商品成功").setCode(200);
    }
    @ApiOperation(value = "根据菜品名称模糊查询拥有该菜品的店铺")
    @ApiImplicitParam(name = "productName",value = "菜品名称")
    @GetMapping("/searchStoreByProductName")
    @RequireRole({"商家","学生"})
    public Result searchStoreByProductName(String productName,
                                           @NotNull(message = "页码不能为空")
                                           @Positive(message = "页码必须为正整数")
                                           @RequestParam
                                           Integer PageNum,
                                           @NotNull(message = "页大小不能为空")
                                           @Positive(message = "页大小必须为正整数")
                                           @RequestParam
                                           Integer PageSize) throws Exception {
        PageInfo<StoreListDTO> stores=storeProductService.searchStoreByProductName(productName,PageNum,PageSize);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setCode(200).setData(stores);
    }
}

